//
//  ScoreDetailViewController.m
//  edu
//
//  Created by baobaodp on 13-8-26.
//  Copyright (c) 2013年 baobaodp. All rights reserved.
//

#import "ScoreDetailViewController.h"
#import "ScoreDetailCell.h"
#import "ScoreTeacherViewController.h"
#import "DBbase.h"
#import "EduViewController.h"

@interface ScoreDetailViewController ()

@end

@implementation ScoreDetailViewController

@synthesize teacher;
@synthesize lessonName;
@synthesize score;
@synthesize m_record;
@synthesize classAvg;
@synthesize classMax;
@synthesize classMin;
@synthesize greadAvg;
@synthesize greadMax;
@synthesize greadMin;
@synthesize studentInfo;
@synthesize stuName;
@synthesize imgView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setTitle:@"成绩"];
    [self doSetData];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    // Navigation logic may go here. Create and push another view controller.
	
	ScoreTeacherViewController *detailViewController = [[ScoreTeacherViewController alloc] initWithNibName:@"ScoreTeacherViewController" bundle:nil];
    
	// Pass the selected object to the new view controller.
	
	if (tableView == self.searchDisplayController.searchResultsTableView){
        //		detailViewController.currentLineName = [filteredListContent objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = [[CDataContainer Instance].lineNameArray indexOfObject:[filteredListContent objectAtIndex:indexPath.row]];
	}
	else{
        //		detailViewController.currentLineName = [[CDataContainer Instance].lineNameArray objectAtIndex:indexPath.row];
        //		detailViewController.currentLineIndex = indexPath.row;
	}
    
    [self setTitle:@"成绩"];
	[self.navigationController pushViewController:detailViewController animated:YES];
    
    
}

- (IBAction)teacherClick:(id)sender
{
    ScoreTeacherViewController *detailViewController = [[ScoreTeacherViewController alloc] initWithNibName:@"ScoreTeacherViewController" bundle:nil];
    
    detailViewController.tch_Id =[m_record getFieldValueByName:@"tch_Id"];
	[self.navigationController pushViewController:detailViewController animated:YES];
}

-(void) doSetData
{
    self.lessonName.text = [m_record getFieldValueByName:@"lesson_Title"];
    self.score.text = [NSString stringWithFormat:@"%@ 分",[m_record getFieldValueByName:@"R_Souce"]]  ;
    
    
    [self.teacher setTitle:[m_record getFieldValueByName:@"tch_Name"] forState:UIControlStateNormal];
    
    NSString *class_Id = [self.studentInfo getFieldValueByName:@"class_Id"];
    NSString *class_Ym = [self.studentInfo getFieldValueByName:@"class_Ym"];
    NSString *class_Div = [self.studentInfo getFieldValueByName:@"class_Div"];
    
    NSString *querySql = [NSString stringWithFormat:@"select * from T_Statistics where ptn_Id ='%@' and lesson_ID ='%@' and (class_Id = '%@' or (class_Id is null and class_Ym = '%@' and class_Div = '%@'))" ,[m_record getFieldValueByName:@"ptn_Id"],[m_record getFieldValueByName:@"lesson_ID"],class_Id,class_Ym,class_Div];
    
    const char *sql = [querySql UTF8String];
    
    
     NSMutableArray * stt = getDbRecords(sql,@"T_Statistics",[NSArray arrayWithObjects:@"R_Id", nil]);
    
    self.stuName.text =[self.studentInfo getFieldValueByName:@"stu_Name"];
    
    UIImage *img=[UIImage imageWithData:[self.studentInfo getFieldDataByName:@"stu_Img"]];
    self.imgView.image =img;
    
    for(Record *recordStt in stt){
        if([[recordStt getFieldValueByName:@"stt_Div"] isEqual:@"1"]){
            self.classAvg.text =[recordStt getFieldValueByName:@"stt_Avg"];
            self.classMax.text =[recordStt getFieldValueByName:@"stt_Max"];
            self.classMin.text =[recordStt getFieldValueByName:@"stt_Min"];
            
        }else if([[recordStt getFieldValueByName:@"stt_Div"] isEqual:@"2"]){
            self.greadAvg.text =[recordStt getFieldValueByName:@"stt_Avg"];
            self.greadMax.text =[recordStt getFieldValueByName:@"stt_Max"];
            self.greadMin.text =[recordStt getFieldValueByName:@"stt_Min"];

        }
    }
}

- (void) setData:(Record *) record
{
    self.m_record = record;
    
}

@end
